package com.py.korona.ui.activities.room;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import com.py.korona.R;
import com.py.korona.ui.controls.fragments.CustomFragment;

public class RoomDetailFragmentViewer extends CustomFragment {
    private RoomDetailFragmentListener listener;

    public RoomDetailFragmentListener getListener() {
        return listener;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        boolean result = false;
        switch (item.getItemId()) {
            case android.R.id.home: {
                listener.fragmentOnCloseAction(this);
                result = true;
                break;
            }
            case R.id.DetailsMenuItemDelete: {
                listener.fragmentOnDeleteAction(this);
                listener.fragmentOnCloseAction(this);
                break;
            }
            case R.id.DetailsMenuItemEdit: {
                listener.fragmentOnEditAction(this);
                break;
            }
            default: {
                result = super.onOptionsItemSelected(item);
                break;
            }
        }
        return result;
    }

    @Override
    public int optionsMenuResourceId() {
        return R.menu.activity_room_detail_view;
    }

    public void setListener(RoomDetailFragmentListener listener) {
        this.listener = listener;
    }

    @Override
    public void viewDidLoad() {
        /* load name */
        TextView mTitleText = (TextView) getActivity().findViewById(R.id.activity_room_fragment_view_edit_name);
        mTitleText.setText(listener.fragmentGetRoomName(this));
        /* load description */
        TextView mTitleDescription = (TextView) getActivity().findViewById(R.id.activity_room_fragment_view_edit_description);
        mTitleDescription.setText(listener.fragmentGetRoomDescription(this));
        /* load description */
        RatingBar mTitleRatingBar = (RatingBar) getActivity().findViewById(R.id.activity_room_fragment_view_rating);
        mTitleRatingBar.setRating(listener.fragmentGetRoomRating(this));
        /* load category */
        TextView mCategoryText = (TextView) getActivity().findViewById(R.id.activity_room_fragment_view_edit_category);
        mCategoryText.setText(listener.fragmentGetRoomCategories(this));
        /* load image */
        ImageView imageView = (ImageView) getActivity().findViewById(R.id.DetailRoomPreviewImage);
        Bitmap bitmap = listener.fragmentGetRoomBitmap(this);
        if (null == bitmap) {
            imageView.setImageResource(android.R.color.transparent);
        } else {
            imageView.setImageBitmap(bitmap);
        }
    }

    @Override
    public int viewLayoutId() {
        return R.layout.activity_room_detail_fragment_view;
    }
}
